package com.rcp.manager;/* Restaurant Control Program >> Restaurant Manager */


import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;


@WebServlet("/MenusTable")
public class MenusTable extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Connection connection = Nexus.connection;

	
    public MenusTable() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Print out Employees
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		ArrayList<Menu> menus = new ArrayList<Menu>();
		
		//Check if connection is active
		if(this.connection == null){
			establishConnection();
		} else
			try {
				if(this.connection.isClosed()){
					establishConnection();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		

		//Get Menus		
		try {
			String preparedQuery = 
					"SELECT menu FROM Menus;";
			
			PreparedStatement ps = (PreparedStatement) connection.prepareStatement(preparedQuery);
			
			ResultSet results = ps.executeQuery();

			//Reading Object from BLOB
			//http://hans.liss.pp.se/node/700
			//int menuID = 0;
			
			try {
				while(results.next()){
					//menuID = results.getInt("menuId");

					InputStream is = results.getBlob("menu").getBinaryStream();
					ObjectInputStream ois = new ObjectInputStream(is);
					Object o = null;
					
					o = ois.readObject();
					menus.add((Menu)o);
					
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} 


		} catch (SQLException e) {
			e.printStackTrace();
		}

		
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");

		out.println("<html>");
		
		out.println("<head>");

		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
		
		out.println("<link href=\"resources/CSS/Featured.css\" rel=\"stylesheet\" type=\"text/css\"/>");

		out.println("<title>Employee Creation</title>");

		out.println("</head>");

		out.println("<body>");

		//Get table dimensions
		int dimensions = getTableDimensions(menus.size());
		
		if(dimensions > 3){
			dimensions = 3;
		}
		
		out.println("<div class='container'>");
		
		out.println("<form name='chosen' action='MenuManagerServlet' method='get'>");
		
		out.println("<table class='staffTable'>");
		

		//Loop & Print through employees array
		Menu current = null;
		int counter = 0;
		
		
		for(int i = 0; i < menus.size(); i++){
			current = menus.get(i);
			
			//System.out.println("Current:" + current.getMenuID());
			
			if(counter == dimensions || i == 0){
				out.println("<tr>");
			}
			
				out.println("<td class='block'>");
				
				out.println("<button type='submit' name='selected' class='cell' value='" + current.getMenuID() + 
							"'>"  + current.getName() + "</button>");
				out.println("<br /><br />");
				
				out.println("</td>");
				
				counter++;
			
			if(counter == dimensions){
				out.println("</tr>");
				counter = 0;
			}
		}
		
		
		out.println("</table>");
		
		out.println("</form>");
		
		out.println("</div>");
		
		out.println("</body>");
		
		out.println("</html>");
		
		out.close();
	
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	
	private int getTableDimensions(int size){
		int dimensions = 0;
		
		if(size > 0){
			dimensions = (int) Math.floor(Math.sqrt(size));
			
			if(dimensions < 1){
				return 1;
			}
			
			return dimensions;
		}
		
		return 0;
	}
	
	public void establishConnection(){
		
		String dbURL = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
		String dbName = "";
		String username = "cs437group01";
		String password = "VHj#dj!v";
		String driver = "com.mysql.jdbc.Driver";
		 
		/*
		String dbURL = "jdbc:mysql://localhost:3306/";
		String dbName = "rcp";
		String username = "root";
		String password = "sesame";
		String driver = "com.mysql.jdbc.Driver";
		*/
		
		try {
			Class.forName(driver).newInstance();
			Nexus.connection = (Connection) DriverManager.getConnection(dbURL + dbName, username, password);
			this.connection = Nexus.connection;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
